package com.hfzy.ihk.web.ivr.support.callin.handler;

import com.alibaba.fastjson.JSONObject;
import com.hfzy.ihk.facade.ivr.vo.CallInto;
import com.hfzy.ihk.web.ivr.biz.CallIntoStartBiz;
import com.hfzy.ihk.web.ivr.constant.UserMsgType;
import com.hfzy.ihk.web.ivr.support.callin.event.CallIntoEvent;
import com.hfzy.ihk.web.ivr.support.disruptor.base.DisruptorHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


/**
 * @Auther: Administrator
 * @Date: 2018/11/9 11:35
 * @Description:*/

@Component
@Scope("prototype")
public class DongjinCallIntoHandler implements DisruptorHandlerAdapter<CallIntoEvent> {


    Logger logger = LoggerFactory.getLogger(DongjinCallIntoHandler.class);

    @Autowired
    CallIntoStartBiz callIntoStartBiz;

    @Override
    public void onEvent(CallIntoEvent event, long sequence, boolean endOfBatch) throws Exception {
        try {
            procces(event.getData());
        } catch (Exception e) {
            logger.error("DongjinCallIntoHandler exception",e);
        }
    }

    @Override
    public void onEvent(CallIntoEvent event) throws Exception {
        try {
            Long old = System.currentTimeMillis();
            procces(event.getData());
            old = System.currentTimeMillis()-old;
            logger.debug("handle use time:{}",old);

        } catch (Exception e) {
            logger.error("DongjinCallIntoHandler exception",e);
        }
    }

    private void procces(CallInto callInto) {
        try {
            if(UserMsgType._302.equals(callInto.getvCallState())){
                callIntoStartBiz.callInto(callInto);
            }
        } catch (Exception e) {
            logger.error("呼入异常,data:{}，错误信息：{}，",JSONObject.toJSONString(callInto),e);
        }
    }



}
